﻿using Microsoft.AspNetCore.Mvc;
using StudyCompanion.Commons.Cache;
using StudyCompanion.Commons.Result;
using StudyCompanion.Core.Aliyun;
using StudyCompanion.Core.API;
using StudyCompanion.Core.Share;

namespace StudyCompanion.WebApi.Controllers;

/// <summary>
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class HomeController : ApiController
{
    private readonly ICacheService _cacheService;
    private readonly IUserContext _userContext;

    /// <summary>
    /// </summary>
    /// <param name="userContext"></param>
    /// <param name="cacheService"></param>
    public HomeController(IUserContext userContext
        , ICacheService cacheService)
    {
        _userContext = userContext;
        _cacheService = cacheService;
    }

    /// <summary>
    ///     心跳检测
    /// </summary>
    /// <returns></returns>
    [HttpGet("Heartbeat")]
    public CommonResult<bool> Heartbeat()
    {
        var key = _cacheService.GetByKey<string>(_userContext.Id.ToString());
        if (!string.IsNullOrEmpty(key))
            if (key == _userContext.ClientId)
                return true;
        return false;
    }

    /// <summary>
    ///     oss上传签名
    /// </summary>
    /// <param name="dir">文件上传的前缀</param>
    /// <returns></returns>
    [HttpGet("OssSign")]
    public CommonResult<object> OssUploadSign([FromQuery] string dir)
    {
        return new CommonResult<object>(OSS.OssSign(dir));
    }
}